using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif

[Serializable]
public class BTreeEditorData : ScriptableObject
{
    public List<string> btreesName;
    //public List<BTNodeGraph> btreesGraph;

    //当前复制的节点
    protected BTNodeBase nodeCopyed;
    //是复制还是剪贴 true为复制 false为剪贴
    protected bool isCopy = true;

    private GUIStyle _labelStyle;
    public GUIStyle labelStyle
    {
        set{
            _labelStyle = value;
        }
        get
        {
            if(_labelStyle == null)
            {
                 _labelStyle = new GUIStyle();
                 _labelStyle.normal.background = null;    //这是设置背景填充的
                 _labelStyle.normal.textColor = new Color(1, 1, 1);   //设置字体颜色的
                 _labelStyle.fontSize = 15;
                 _labelStyle.alignment = TextAnchor.UpperCenter;  
                 _labelStyle.wordWrap = true;
                 return _labelStyle;
            }
            return _labelStyle;
        }
    }

    //保存编辑器数据的路径
    static public string pathTreeEditorData = "Assets/Editor/Data/NodeEditor/";
    ///保存游戏数据BTree的位置
    static public string pathTreeGameData = "Assets/Resources/GameData/AI/Tree/";
    //GUISkins路径
    static public string pathGUISkins = "Assets/Editor/BTreeEditor/GUISkins/EditorSkins/NoteEditorSkin.guiskin";

    static private BTreeEditorData _instance;
    static public BTreeEditorData instance
    {
        get{
            if(_instance == null){
                initData();
            }
            return _instance;
        }
        set{
            _instance = value;
        }
    }

    ///初始化编辑器数据
    public static BTreeEditorData initData()
    {
        BTreeEditorData data = null;
        string dataPath = pathTreeEditorData + "BTreeEditorData.asset";
        data = AssetDatabase.LoadAssetAtPath<BTreeEditorData>(dataPath);
        if(data == null)
        {
            data = ScriptableObject.CreateInstance<BTreeEditorData>();
            data.btreesName = new List<string>();
           // data.btreesGraph = new List<BTNodeGraph>();
            UtilsEditor.CreateAsset(data,"BTreeEditorData.asset",pathTreeEditorData);

            data.labelStyle = new GUIStyle();
            data.labelStyle.normal.background = null;    //这是设置背景填充的
            data.labelStyle.normal.textColor = new Color(1, 1, 1);   //设置字体颜色的
            data.labelStyle.fontSize = 20;
            data.labelStyle.wordWrap = true;

        }

        BTreeEditorData.instance = data;
        EditorUtility.SetDirty(data);
        return data;
    }
    
    public bool newTree(string wantedName, string treeDes)
    {
        Debug.Log("Creating New Graph...");
        BTNodeGraph curGraph = ScriptableObject.CreateInstance<BTNodeGraph>();
        if(curGraph != null)
        {
            curGraph.GraphName = wantedName;
            curGraph.treeDes = treeDes;
            curGraph.InitGraph();
            if(this.btreesName.Contains(wantedName))
            {
                EditorUtility.DisplayDialog("Note Message:", "已存在同名BTree", "OK");
                return false;
            }
            this.addBTree(wantedName, curGraph);
            EditorUtility.SetDirty(this);
            UtilsEditor.CreateAsset(curGraph, wantedName + ".asset", pathTreeEditorData + wantedName + "/");

            BTNodeEditorWindow curWindow = EditorWindow.GetWindow<BTNodeEditorWindow>();
            if(curWindow != null)
            {
                curWindow.curGraph = curGraph;
            }

            return true;
        }
        else
        {
            EditorUtility.DisplayDialog("Note Message:", "Unable to create new graph", "OK");
            return false;
        } 
    }

    ///向list中添加btree
    private void addBTree(string wantedName, BTNodeGraph btree)
    {
        this.btreesName.Add(wantedName);
        //this.btreesGraph.Add(btree);
    }

    //从list中获取btree
    public BTNodeGraph getBTreePath(string treeName)
    {
        int index = this.btreesName.IndexOf(treeName);
        if(index < 0)
        {
            return null;
        }

        BTNodeGraph graph = UtilsEditor.loadAsset<BTNodeGraph>(treeName + ".asset", pathTreeEditorData + treeName);
        if(graph == null)
        {
            this.removeBTree(treeName);
        }
        return graph;
    }

    //删除
    public void removeBTree(string treeName)
    {
        int index = this.btreesName.IndexOf(treeName);
        if (index < 0)
        {
            return ;
        }
        this.btreesName.RemoveAt(index);
    }

    ///是否有被复制的节点
    public bool hasCopyNode(out bool isCopy)
    {
        if(this.nodeCopyed == null)
        {
            isCopy = true;
            return false;
        }
        else
        {
            isCopy = this.isCopy;
            return true;
        }
    }

    ///设置复制节点 isCopy=true为复制 false为剪贴
    public void copyNode(BTNodeBase node,bool isCopy)
    {
        this.nodeCopyed = node;
        this.isCopy = isCopy;
    }

    ///粘贴节点作为parentNode的子节点
    public void pasteNode(BTNodeBase parentNode)
    {
        if(this.nodeCopyed == null)
        {
            return;
        }
        BTNodeBase copyNode = BTNodeUtils.copyNode(this.nodeCopyed, parentNode.parentGraph);
        parentNode.addChild(copyNode);

         Vector2 offset = new Vector2(parentNode.nodeRect.x - this.nodeCopyed.nodeRect.x + parentNode.nodeRect.width+20,
                                     parentNode.nodeRect.y - this.nodeCopyed.nodeRect.y + parentNode.nodeRect.height+20);
        copyNode.moveAllNode(offset);

        if(!isCopy)
        {
            BTNodeUtils.removeNode(this.nodeCopyed,this.nodeCopyed.parentGraph);
        }
        this.nodeCopyed = null;
        this.isCopy = true;
    }

    ///粘贴节点,在preNode前插入
    public void pasteBeforeNode(BTNodeBase preNode)
    {
        if(this.nodeCopyed == null)
        {
            return;
        }
        BTNodeBase parentNode = preNode.nodeParent;
        BTNodeBase copyNode = BTNodeUtils.copyNode(this.nodeCopyed, parentNode.parentGraph);
        BTNodeUtils.InsertChildBefore(preNode, copyNode);

         Vector2 offset = new Vector2(parentNode.nodeRect.x - this.nodeCopyed.nodeRect.x + parentNode.nodeRect.width+20,
                                     parentNode.nodeRect.y - this.nodeCopyed.nodeRect.y + parentNode.nodeRect.height+20);
        copyNode.moveAllNode(offset);

        if(!isCopy)
        {
            BTNodeUtils.removeNode(this.nodeCopyed, this.nodeCopyed.parentGraph);
        }
        this.nodeCopyed = null;
        this.isCopy = true;
    }
}